//
//  SharePopView.m
//  TenMinDemo
//
//  Created by Shawn Li on 2021/3/10.
//  Copyright © 2021 CYXiang. All rights reserved.
//

#import "SharePopView.h"
@interface SharePopView()
@property (weak, nonatomic) IBOutlet UIImageView *shareImg;
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bgBottom; // 530,0
@end
@implementation SharePopView


INITFROMXIB

- (void)awakeFromNib{
    [super awakeFromNib];
    self.bgView.backgroundColor = MainColor;
    CGRect frame = self.bgView.bounds;
    frame.size.width = SCREEN_WIDTH;
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect: frame byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10,10)];
      //创建 layer
      CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
      maskLayer.frame = frame;
      //赋值
      maskLayer.path = maskPath.CGPath;
    self.bgView.layer.mask = maskLayer;
    LRViewBorderRadius(self.shareImg, 8, 0, [UIColor clearColor]);
    
    self.shareImg.image = [UIImage imageNamed:[LanguageManager isEnglish] ? @"share_hb_en" :@"share_hb_zh"];
}

- (void)showInView:(UIView *)view{
    [view addSubview:self];
    [self show];
}


- (void)show{
    self.backgroundColor = [UIColor clearColor];
    self.bgBottom.constant = -530;
    [UIView animateWithDuration:0.35 animations:^{
        self.backgroundColor = [[UIColor alloc] initWithWhite:0 alpha:0.8];
        self.bgBottom.constant = 0;
        [self layoutIfNeeded];
        } completion:^(BOOL finished) {
            
        }];
    
}

- (void)hide{
    [self endEditing:YES];
    [UIView animateWithDuration:0.35 animations:^{
       
        self.backgroundColor = [UIColor clearColor];
        self.bgBottom.constant = -530;
        [self layoutIfNeeded];
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    
}


- (IBAction)shareAction:(UIButton *)sender{
    if (self.actionBlock) {
        self.actionBlock(sender.tag - 1000);
    }
    [self hide];
}
- (IBAction)cancelAction:(id)sender {
    [self hide];
}

- (IBAction)saveAction:(id)sender {
    if (self.shareImg.image) {
        UIImageWriteToSavedPhotosAlbum(self.shareImg.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    }
}

#pragma mark -- <保存到相册>
-(void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    NSString *msg = nil ;
    if(error){
        msg = @"保存图片失败" ;
    }else{
        msg = @"保存图片成功" ;
    }
    LCInfoHUD(msg, 1.5);
}

@end
